const { Sequelize } = require("sequelize");
const { isRequired } = require("../utils");

module.exports = (sequelize, DataTypes) => {
  const Dict = sequelize.define(
    "Dict",
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: Sequelize.UUIDV4,
        //是否允许为空
        allowNull: false,
        //是否为主键
        primaryKey: true,
        //备注
        comment: "字典组id",
      },
      dictName: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: "字典名称",
        validate: isRequired("字典名称"),
      },
      dictCode: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: "字典编码",
        unique: {
          msg: "字典编码已存在",
        },
        validate: isRequired("字典编码"),
      },
      remark: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: "字典组描述",
      },
    },
    {
      timestamps: true,
      tableName: "system_dict",
      createdAt: "createTime",
      updatedAt: "updateTime",
      comment: "字典组信息表",
    }
  );
  Dict.associate = function (db) {
    Dict.hasMany(db.DictItem, {
      foreignKey: "dictId",
      as: "options",
    });
  };
  return Dict;
};
